﻿using System.Windows;
using Baosight.ZNH.ZMCommon;
using System;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core;
using Baosight.Platform.Core.UI;
using Microsoft.Practices.Unity;
using Baosight.ZNH.ZMCS.UI.Utils;
using System.Collections.Generic;
using System.Windows.Controls;
using Telerik.Windows.Controls.GridView;
using Baosight.ZNH.ZMCS.UI.ChildWindows;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Telerik.Windows.Controls;
using Baosight.Platform.Core.ServiceModel;
using Baosight.ZNH.ZMCS.UI.Download;

namespace Baosight.ZNH.ZMCS.UI
{
    public partial class ZMCS63 : EFViewCmes
    {
        private ProjectModelBlock _block = new ProjectModelBlock();//私有化下拉列表

        public ZMCS63()
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
        }

        /// <summary>
        /// 修改前设置属性
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.grid.GetCheckedEIRows();
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        /// <param name="initData"></param>
        public override void Initialize(EIInfo initData)
        {
            initData.AddBlock(_block.GetProjectDesionType());//列表区
            this.INQU_fdRoadwayTypeId.Value = "";//查询区下拉框
            base.Initialize(initData);
        }

        /// <summary>
        /// 点击新增按钮，根据矿区形式判断是否可以新增矿井记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnInsertButtonClick(object sender, RoutedEventArgs e)
        {
            string _value = this.roadwayNoType.Value == null ? "" : this.roadwayNoType.Value.ToString();

            if (_value.Equals("2"))
            {
                Button _button = sender as Button;
                _button.IsEnabled = false;
                base.TempButton = _button;

                EIInfo eiInfo = new EIInfo();
                eiInfo.Set("service", "ZMCS63");
                eiInfo.Set("method", "getNewNumber");
                eiInfo.Set("fdEngineeringdesignId", this.fdParentId.Value.ToString());

                HttpServiceAgent agent = new HttpServiceAgent();
                agent.CallService(eiInfo, CallServiceCompleted);
            }
            else
            {
                EIInfo eiInfo = new EIInfo();
                base.OnInsertButtonClickPopup(eiInfo, "巷道-新增", "insert", null, sender, e);
            }

        }

        /// <summary>
        /// 访问后台的回调
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CallServiceCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            if (e.EIInfo == null || e.EIInfo.GetStatus() == -1)
            {
                base.OpenEFWindowAlert("操作失败！", true);
            }
            else
            {
                this.OpenPopup(e.EIInfo, "巷道-新增", "insert", "1");
            }
        }

        /// <summary>
        /// 点击修改按钮，弹出新增页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnUpdateButtonClick(object sender, RoutedEventArgs e)
        {            
            TempButton = sender as Button;
            TempButton.IsEnabled = false;

            List<EIRow> EFGridCheckedRows = this.grid.CheckedEIRows;
            int count = EFGridCheckedRows.Count;
            if (count <= 0)
            {
                BaseTools.OpenEFWindowAlert("请选择一条记录进行修改！", WindowAlertClosed);
                return;
            }
            else if (count > 1)
            {
                BaseTools.OpenEFWindowAlert("最多只能选择一条记录进行修改！", WindowAlertClosed);
                return;
            }
            else
            {
                EIInfo info = new EIInfo();
                info.Attributes.Add(EIConstants.ServiceNameKey, "ZMCS63");
                info.Attributes.Add(EIConstants.MethodNameKey, "queryFiles");
                info.Attributes.Add("fdKeyId", EFGridCheckedRows[0]["fdKeyId"]);

                HttpServiceAgent agent = new HttpServiceAgent();
                agent.CallService(info, OnQueryFilesCompleted);
            }

        }

        /// <summary>
        /// 修改按钮中查询附件信息的回调函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnQueryFilesCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            if (e.EIInfo.Status != -1)
            {
                IList<EIRow> _list = this.grid.GetCheckedEIRows() == null ? new List<EIRow>() : this.grid.GetCheckedEIRows();

                EIInfo eiInfo = new EIInfo();
                EIBlock eIBlock = new EIBlock("result");
                EIRow row = new EIRow();
                if (_list.Count > 0)
                {
                    row = BaseTools.GetEIRowByOldEIRow(this.EIDataContext.GetBlock("result").GetColumns(), _list[0]);
                }
                eIBlock.SetRow(0, row);
                eiInfo.AddBlock(eIBlock);
                eiInfo.AddBlock(e.EIInfo.GetBlock("filesresult"));

                base.OnUpdateButtonClickPopup(eiInfo, "巷道-修改", "update", null, TempButton, null);
            }
            else
            {
                BaseTools.OpenEFWindowAlert("查询附件失败！", null);
                this.TempButtonOpen(true);
            }
        }

        /// <summary>
        /// 点击查看按钮执行的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton link = sender as HyperlinkButton;

            EIRow row = (link.ParentOfType<GridViewRow>().Item as UIBaseEntity).GetEIRow();

            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");
            eIBlock.SetRow(0, row == null ? new EIRow() : row);
            eiInfo.AddBlock(eIBlock);

            base.OnHyperlinkButtonClickPopup(eiInfo, "巷道-查看", "detail", null, sender, e);
        }

        /// <summary>
        /// 新增或修改之前的数据传输
        /// 构造新增页面内容、弹出页面
        /// </summary>
        /// <param name="eIInfo"></param>
        /// <param name="title"></param>
        public override void OpenPopup(EIInfo _eIInfo, string _title, string _dialogType, string _args)
        {
            RoadWay _roadWay = new RoadWay(_dialogType);//构造一个新的修改页面
            _eIInfo.AddBlock(_block.GetProjectDesionType());//工程设计类型
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cs.roadwayType"));//巷道类型
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cs.mineRock"));//煤岩别
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cs.supportingType"));//支护形式
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cs.roadwayUsing"));//巷道用途
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cs.workingFace"));//服务工作面

            _roadWay.roadWayNoType = this.roadwayNoType.Value.ToString();
            if (_args != null && _args.Equals("1"))
            {
                _roadWay._number = _eIInfo.Get("fdNo") == null ? "" : _eIInfo.Get("fdNo").ToString();//生成的编号
            }
            _roadWay.EIDataContext = new Platform.Core.UI.EIDataContext();
            _roadWay.EIDataContext.Update(_eIInfo);

            _roadWay.fdEngineeringdesignId.Value = this.fdParentId.Value;//工程设计ID

            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = _title == null ? "" : _title;//设置Title
            _popup.Content = _roadWay;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(CmesWindow_Closed);
            _popup.ShowDialog();//模态显示弹出窗口
        }

        /// <summary>
        /// 弹出窗口关闭后执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CmesWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                this.grid.Refresh();//刷新Grid区域
                this.UpdateHomePageData(new EIInfo());//发布数据刷新树
            }

            //按钮生效
            base.TempButtonOpen(true);

            //超链接生效
            if (base.TempHyperlink != null)
            {
                base.TempHyperlink.IsEnabled = true;
                base.TempHyperlink = null;
            }
        }

        /// <summary>
        /// 重写基类方法：根据数据的改变更新主页面的数据
        /// </summary>
        /// <param name="data"></param>
        public override void UpdateHomePageData(EIInfo data)
        {
            // 数据发布
            IEventAggregator eventAggreator = Container.Resolve<IEventAggregator>();
            EIDataPublishedEvent dataPublishedEvent = eventAggreator.GetEvent<EIDataPublishedEvent>();
            dataPublishedEvent.Publish(data);
        }

        /// <summary>
        /// 点击下载附件按钮执行的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DownLoad_HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            TempHyperlink = sender as HyperlinkButton;
            TempHyperlink.IsEnabled = false;
            EIRow row = (TempHyperlink.ParentOfType<GridViewRow>().Item as UIBaseEntity).GetEIRow();
            EIInfo info = new EIInfo();
            info.Attributes.Add(EIConstants.ServiceNameKey, "ZMCS63");
            info.Attributes.Add(EIConstants.MethodNameKey, "queryFiles");
            info.Attributes.Add("fdKeyId", row["fdKeyId"]);

            HttpServiceAgent agent = new HttpServiceAgent();
            agent.CallService(info, OnQueryDownloadFilesCompleted);
        }

        /// <summary>
        /// 下载附件按钮中查询附件的回调函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnQueryDownloadFilesCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            DownloadView view = new DownloadView();
            view.EIDataContext = new Platform.Core.UI.EIDataContext();
            view.EIDataContext.Update(e.EIInfo);

            CmesWindows _win = new CmesWindows();//构造一个弹出窗口
            _win.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _win.Header = view.eTitle;//设置Title
            _win.Content = view;
            _win.Closed += delegate { if (TempHyperlink != null) { TempHyperlink.IsEnabled = true; TempHyperlink = null; } };
            _win.ShowDialog();//模态显示弹出窗口
        }
    }
}
